﻿using Common.Communal;
using ERP.Domain.Scheduling;
using Scheduling.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scheduling.Services.TeamTypes
{
    public class TeamTypeServices : ITeamTypeServices
    {
        public TeamTypeServices(ITeamTypeRepository teamTypeRepository)
        {
            TeamTypeRepository = teamTypeRepository;
        }

        public ITeamTypeRepository TeamTypeRepository { get; }

        public async Task<ApiResult<int>> AddTeamType(TeamType teamType)
        {
            ApiResult<int> result = new ApiResult<int>();
            
            try
            {
                // 非空判断
                if (teamType == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型信息不能为空";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(teamType.TeamTypeName))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型名称不能为空";
                    return result;
                }

                // 添加时始终为班组类型编码生成全球唯一标识符（GUID）
                teamType.TeamTypeCode = Guid.NewGuid().ToString("N");

                result.Code = EnumCode.Ok;
                result.Message = "班组类型添加成功";
                var list = await TeamTypeRepository.AddAsync(teamType);
                result.Data = list;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"添加班组类型失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> UpdTeamType(TeamType teamType)
        {
            ApiResult<int> result = new ApiResult<int>();
            
            try
            {
                // 非空判断
                if (teamType == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型信息不能为空";
                    return result;
                }

                // 必填字段验证
                if (teamType.TeamTypeId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型ID不能为空或无效";
                    return result;
                }

                if (string.IsNullOrWhiteSpace(teamType.TeamTypeName))
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型名称不能为空";
                    return result;
                }

                // 检查班组类型是否存在
                var existingTeamType = await TeamTypeRepository.GetModelAsync(teamType.TeamTypeId);
                if (existingTeamType == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "要修改的班组类型不存在";
                    return result;
                }

                // 不允许修改班组类型编码，保留数据库中的原值
                teamType.TeamTypeCode = existingTeamType.TeamTypeCode;

                // 将修改字段赋值到已跟踪的实体上，避免EF重复跟踪同键实例
                existingTeamType.TeamTypeName = teamType.TeamTypeName;
                // existingTeamType.TeamTypeCode 已在上方保持原值
                // 如有其他可修改字段，在此一并赋值

                result.Code = EnumCode.Ok;
                result.Message = "班组类型修改成功";
                var list = await TeamTypeRepository.Update(existingTeamType);
                result.Data = list;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"修改班组类型失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> DeleteTeamType(int teamTypeId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                // 根据ID获取班组类型信息
                var teamType = await TeamTypeRepository.GetModelAsync(teamTypeId);
                if (teamType == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "班组类型不存在";
                    return result;
                }

                // 将IsDelete状态设置为true（逻辑删除）
                teamType.IsDelete = true;
                
                // 更新班组类型信息
                var updateResult = await TeamTypeRepository.Update(teamType);
                
                result.Code = EnumCode.Ok;
                result.Message = "班组类型删除成功";
                result.Data = updateResult;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"删除班组类型失败：{ex.Message}";
            }
            
            return await Task.FromResult(result);
        }
    }
}
